package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.KnowledgeBaseTypeDto;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.KnowledgeTypeApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.QueryKnowledgeTypeDetailReq;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.QueryKnowledgeTypeDetailResp;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.QueryKnowledgeTypeTreeListReq;
import com.huishi.gx.ib.grpc.lib.ib.app.knowledge.v1.QueryKnowledgeTypeTreeListResp;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/4/7
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class KnowledgeTypeApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final KnowledgeTypeApiService INSTANCE = new KnowledgeTypeApiService();
    }

    public static KnowledgeTypeApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private KnowledgeTypeApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private KnowledgeTypeApiGrpc.KnowledgeTypeApiBlockingStub getStub(ManagedChannel channel) {
        return KnowledgeTypeApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询文章分类列表
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryKnowledgeTypeTreeListResp> queryKnowledgeTypeTreeList(
            CallBack<QueryKnowledgeTypeTreeListResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryKnowledgeTypeTreeListResp>(callBack) {
            @Override
            protected QueryKnowledgeTypeTreeListResp doRequestData(ManagedChannel channel) {
                QueryKnowledgeTypeTreeListReq message = QueryKnowledgeTypeTreeListReq.newBuilder()
                        .setComQuery(ComQuery
                                .newBuilder()
                                .setPage(1)
                                .setPageSize(10000)
                                .build())
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                QueryKnowledgeTypeTreeListResp response = null;
                try {
                    response = getStub(channel).queryKnowledgeTypeTreeList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryKnowledgeTypeTreeList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 查询文章分类列表
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryKnowledgeTypeTreeListResp> queryKnowledgeTypeTreeList(
            String parentId,
            CallBack<QueryKnowledgeTypeTreeListResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryKnowledgeTypeTreeListResp>(callBack) {
            @Override
            protected QueryKnowledgeTypeTreeListResp doRequestData(ManagedChannel channel) {
                QueryKnowledgeTypeTreeListReq message = QueryKnowledgeTypeTreeListReq.newBuilder()
                        .setComQuery(ComQuery
                                .newBuilder()
                                .setPage(1)
                                .setPageSize(10000)
                                .build())
                        .setKnowledgeBaseTypeDto(KnowledgeBaseTypeDto
                                .newBuilder()
                                .setParentId(parentId)
                                .build())
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                QueryKnowledgeTypeTreeListResp response = null;
                try {
                    response = getStub(channel).queryKnowledgeTypeTreeList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryKnowledgeTypeTreeList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 查看文章分类详情
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryKnowledgeTypeDetailResp> queryKnowledgeTypeDetail(CallBack<QueryKnowledgeTypeDetailResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryKnowledgeTypeDetailResp>(callBack) {
            @Override
            protected QueryKnowledgeTypeDetailResp doRequestData(ManagedChannel channel) {
                QueryKnowledgeTypeDetailReq message = QueryKnowledgeTypeDetailReq.newBuilder()
                        .build();
                Logger.d(message);
                QueryKnowledgeTypeDetailResp response = null;
                try {
                    response = getStub(channel).queryKnowledgeTypeDetail(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryKnowledgeTypeDetail");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

}
